package function;

import Mysql.ShareData;
import View.*;
import client.ClientManager;
import instruction.*;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.shape.Circle;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicReference;


public class MainFunction {
    private static Friend friend;
    private static Group group;
    private static Variety variety;
    private static  String imagePath;
    //获取组件
    public static Object $(window window, String id) {
        return (Object) window.getRoot().lookup("#" + id);
    }
    public static void mainExec(AddFriendWindow addFriendWindow, MainWindow mainWindow, CreateGroupWindow createGroupWindow, AddGroupWindow addGroupWindow,SelectHeadWindow selectHeadWindow, ClientManager clientManager) throws IOException{
        //加载好友信息,搜索好友
        ((MenuButton) $(mainWindow, "searchButton")).getItems().get(0).setOnAction(event -> {
            ((Label) $(mainWindow, "searchTip")).setText("");
            String account = ((TextField) $(mainWindow, "searchText")).getText();
            if (account.equals("")) {
                ((Label) $(mainWindow, "searchTip")).setText("搜索内容不能为空");
            } else {
                Search search = new Search(account, "1");
                Message message = new Message("search");
                message.setSearch(search);
                clientManager.getSend().send(message);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ie) {
                }
                User friend1 = ShareData.UserMap.get(account);
                User friend2=ShareData.UserMap.get(account);
                if (friend1 != null) {
                    addFriendWindow.setFriend(friend1);
                    addFriendWindow.setFriendData();
                    addFriendWindow.show();
                } else {
                    ((Label) $(mainWindow, "searchTip")).setText("该好友不存在");
                }
            }
        });
        //加载群聊信息,搜索群聊
        ((MenuButton) $(mainWindow, "searchButton")).getItems().get(1).setOnAction(event -> {
            ((Label) $(mainWindow, "searchTip")).setText("");
            String account = ((TextField) $(mainWindow, "searchText")).getText();
            if (account.equals("")) {
                ((Label) $(mainWindow, "searchTip")).setText("搜索内容不能为空");
            } else {
                Search search = new Search(account, "2");
                Message message = new Message("search");
                message.setSearch(search);
                clientManager.getSend().send(message);
                Group group = ShareData.groupMap.get(account);
                if (group != null) {
                    addGroupWindow.setGroup(group);
                    addGroupWindow.setGroupData();
                    addGroupWindow.show();
                } else {
                    ((Label) $(mainWindow, "searchTip")).setText("该群聊不存在");
                }
            }
        });
        //设置点击好友弹出聊天界面的监听事件
        ListView<Friend> friendList = ((ListView) $(mainWindow, "friendList"));
        friendList.getSelectionModel().selectedItemProperty().addListener(listener -> {
            friendList.setOnMouseClicked(event -> {
                if (event.getClickCount() == 2 && event.getButton() == MouseButton.PRIMARY) {
                    int index = friendList.getSelectionModel().getSelectedIndex();
                    friend = mainWindow.getFriendData().get(index);
                    for (ChatWindow chatWindow : ShareData.chatWindowList) {
                        if (chatWindow.getFriend().getMember().getAccount().equals(friend.getMember().getAccount())) {
                            chatWindow.show();
                            break;
                        }
                    }
                }
            });
        });
        //设置点击群聊弹出聊天界面的监听事件
        ListView<Group> groupList = ((ListView) $(mainWindow, "groupList"));
        groupList.getSelectionModel().selectedItemProperty().addListener(listener -> {
            groupList.setOnMouseClicked(event -> {
                if (event.getClickCount() == 2 && event.getButton() == MouseButton.PRIMARY) {
                    int index = groupList.getSelectionModel().getSelectedIndex();
                    group = mainWindow.getGroupData().get(index);
                    for (GroupChatWindow groupChatWindow : ShareData.groupChatWindowList) {
                        if (groupChatWindow.getGroup().getAccount().equals(group.getAccount())) {
                            groupChatWindow.show();
                            break;
                        }
                    }
                }
            });
        });
        //设置点击消息列表弹出聊天界面的监听事件
        ListView<Variety> messageList = ((ListView) $(mainWindow, "messageList"));
        messageList.getSelectionModel().selectedItemProperty().addListener(listener -> {
            messageList.setOnMouseClicked(event -> {
                if (event.getClickCount() == 2 && event.getButton() == MouseButton.PRIMARY) {
                    int index = messageList.getSelectionModel().getSelectedIndex();
                    variety= mainWindow.getMessageData().get(index);
                    //群聊窗口
                    if (variety.getType().equals("group")) {
                        for (GroupChatWindow groupChatWindow : ShareData.groupChatWindowList) {
                            if (groupChatWindow.getGroup().getAccount().equals(variety.getGroup().getAccount())) {
                                groupChatWindow.show();
                                break;
                            }
                        }
                    }else if(variety.getType().equals("friend")){
                        for (ChatWindow chatWindow: ShareData.chatWindowList) {
                            if (chatWindow.getFriend().getMember().getAccount().equals(variety.getFriend().getMember().getAccount())) {
                                chatWindow.show();
                                break;
                            }
                        }
                    }
                }
            });
        });
        //创建群聊事件的监听
        ((Button) $(mainWindow, "createGroupButton")).setOnAction(event -> {
            createGroupWindow.show();
        });
        //点击修改头像

        ((Button)$(mainWindow,"settingButton")).setOnAction(event->{
            User user=mainWindow.getUserData().getUser();
            try {
                ModifyWindow modifyWindow=new ModifyWindow(user);
                modifyWindow.show();
                ((ImageView)$(modifyWindow,"image")).setOnMouseClicked(action->{
                    selectHeadWindow.InitHead();
                    selectHeadWindow.show();
                    ((Button) $(selectHeadWindow, "happyButton")).setOnAction(actionEvent -> {
                        imagePath ="/Picture/headPortrait/喜羊羊.jpeg";
                        ((ImageView) $(mainWindow,"imagePhoto")).setImage(new Image(imagePath, 80, 80, false, false));
                        ((ImageView) $(mainWindow,"imagePhoto")).setClip(getCircle());
                        selectHeadWindow.hide();
                    });
                    ((Button) $(selectHeadWindow, "beautifulButton")).setOnAction(actionEvent -> {
                        imagePath="/Picture/headPortrait/美羊羊.jpg";
                        ((ImageView) $(mainWindow,"imagePhoto")).setImage(new Image(imagePath, 80, 80, false, false));
                        ((ImageView) $(mainWindow,"imagePhoto")).setClip(getCircle());
                        selectHeadWindow.hide();
                    });
                    ((Button) $(selectHeadWindow, "excitedButton")).setOnAction(actionEvent -> {
                        imagePath="/Picture/headPortrait/沸羊羊.jpg";
                        ((ImageView) $(mainWindow,"imagePhoto")).setImage(new Image(imagePath, 80, 80, false, false));
                        ((ImageView) $(mainWindow,"imagePhoto")).setClip(getCircle());
                        selectHeadWindow.hide();
                    });
                    ((Button) $(selectHeadWindow, "lazyButton")).setOnAction(actionEvent -> {
                        imagePath="/Picture/headPortrait/懒羊羊.jpg";
                        ((ImageView) $(mainWindow,"imagePhoto")).setImage(new Image(imagePath, 80, 80, false, false));
                        ((ImageView) $(mainWindow,"imagePhoto")).setClip(getCircle());
                        selectHeadWindow.hide();
                    });
                    ((Button) $(selectHeadWindow, "warmButton")).setOnAction(actionEvent -> {
                        imagePath="/Picture/headPortrait/暖羊羊.jpg";
                        ((ImageView) $(mainWindow,"imagePhoto")).setImage(new Image(imagePath, 80, 80, false, false));
                        ((ImageView) $(mainWindow,"imagePhoto")).setClip(getCircle());
                        selectHeadWindow.hide();
                    });
                    ((Button) $(selectHeadWindow, "villageButton")).setOnAction(actionEvent -> {
                        imagePath="/Picture/headPortrait/村长.jpg";
                        ((ImageView) $(mainWindow,"imagePhoto")).setImage(new Image(imagePath, 80, 80, false, false));
                        ((ImageView) $(mainWindow,"imagePhoto")).setClip(getCircle());
                        selectHeadWindow.hide();
                    });
                    ((Button) $(selectHeadWindow, "grayButton")).setOnAction(actionEvent -> {
                        imagePath="/Picture/headPortrait/灰太狼.jpeg";
                        ((ImageView) $(mainWindow,"imagePhoto")).setImage(new Image(imagePath, 80, 80, false, false));
                        ((ImageView) $(mainWindow,"imagePhoto")).setClip(getCircle());
                        selectHeadWindow.hide();
                    });
                    ((Button) $(selectHeadWindow, "redButton")).setOnAction(actionEvent -> {
                        imagePath="/Picture/headPortrait/红太狼.jpg";
                        ((ImageView) $(mainWindow,"imagePhoto")).setImage(new Image(imagePath, 80, 80, false, false));
                        ((ImageView) $(mainWindow,"imagePhoto")).setClip(getCircle());
                        selectHeadWindow.hide();
                    });
                    ((Button) $(selectHeadWindow, "bananaButton")).setOnAction(actionEvent -> {
                        imagePath="/Picture/headPortrait/蕉太狼.jpeg";
                        ((ImageView) $(mainWindow,"imagePhoto")).setImage(new Image(imagePath, 80, 80, false, false));
                        ((ImageView) $(mainWindow,"imagePhoto")).setClip(getCircle());
                        selectHeadWindow.hide();
                    });
                    ((Button) $(selectHeadWindow, "smallButton")).setOnAction(actionEvent -> {
                        imagePath="/Picture/headPortrait/小灰灰.jpg";
                        ((ImageView) $(mainWindow,"imagePhoto")).setImage(new Image(imagePath, 80, 80, false, false));
                        ((ImageView) $(mainWindow,"imagePhoto")).setClip(getCircle());
                        selectHeadWindow.hide();
                    });
                });
                ((Button)$(modifyWindow,"modify")).setOnAction(actionEvent->{
                    String sign=((TextField)$(modifyWindow,"sign")).getText();
                    String nickname=((TextField)$(modifyWindow,"nickname")).getText();
                    Modify modify=new Modify(nickname,sign,imagePath,user.getAccount());
                    Message message=new Message("modify");
                    message.setModify(modify);
                    clientManager.getSend().send(message);
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }
    public static Circle getCircle(){
        Circle circle = new Circle();
        circle.setRadius(40);
        circle.setCenterX(40);
        circle.setCenterY(40);
        return circle;
    }
}
